package sdk.old;


public class Square
{

	private  String[][] square = new String[3][3];

	public Square(String[][] square) throws Exception
	{
		super();		
		this.square = square;
	}
	
	public Square(){
		String [][] sq = new String[3][3];
		for(int r=0;r<3;r++){
			for(int c=0;c<3;c++){
				sq[r][c]="*";
			}			
		}
		this.square=sq;
	}
	
	public Square(String sequence) throws Exception
	{	
		square = new String[3][3];
		String digit[]=sequence.split(",");
		if (digit.length!=9)
			throw new Exception("Sequence must contain 9 positions");
		for(int i=0;i<digit.length;i++){
			int c=i%3;
			int r=(int) Math.floor(i/3);
			square[r][c]=digit[i];
		}

	}

	public String[][] getSquare()
	{
		return square;
	}

	public void setSquare(String[][] square)
	{
		this.square = square;
	}
	
	public String get(int r, int c)
	{
		return this.square[r][c];
	}
	
	public void set(int r, int c, String v)
	{
		square[r][c]=v;
	}
	
	public boolean validate(){
		String total="";
		for(int r=0;r<3;r++){
			for(int c=0;c<3;c++){
				if (square[r][c].length()!=0 && total.contains(square[r][c]))
					return false;
				total=total+square[r][c];
			}
		}
		if(total.length()!=9)
			return false;
		return true;
	}
	
	public String print(){
		String str="";
		for(int r=0; r<3;r++){
			str=str+"| ";
			for(int c=0; c<3;c++){
				String d =square[r][c];
				if(d.equals("0"))
					d=" ";
				str=str+d+" ";
			}
			str=str+"|\n";
		}
		return str;
	}

	@Override
	public String toString()
	{
		String res="";
		for(int r=0 ; r<3;r++){
			for(int c=0 ; c<3;c++){
				res=res+square[r][c]+" ";
			}
			res=res+"\n";
		}
		return res;
	}
	
	public String toLine()
	{
		String res="";
		for(int r=0 ; r<3;r++){
			for(int c=0 ; c<3;c++){
				res=res+square[r][c];
			}
		}
		return res;
	}

}
